/*
 *
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */
package com.eclub.emotion.rootstone.serializer.io;

import java.io.IOException;

import com.eclub.emotion.rootstone.serializer.CommonDataTypeMap;
import com.eclub.emotion.rootstone.serializer.exception.ForbidSerializeException;


/**
 * 
 * TODO Description
 *
 * @author   jun.yang, Wincor Nixdorf International GmbH
 * @version  $Revision$
 */
class SerializerFactory {

    private final static ISerializerBase arraySerializer = new ArraySerializer();
    private final static ISerializerBase boolSerializer = new BooleanSerializer();
    private final static ISerializerBase byteSerializer = new ByteSerializer();
    private final static ISerializerBase charSerializer = new CharSerializer();
    private final static ISerializerBase dateTimeSerializer = new DateTimeSerializer();
    private final static ISerializerBase decimalSerializer = new DecimalSerializer();
    private final static ISerializerBase doubleSerializer = new DoubleSerializer();
    private final static ISerializerBase enumSerializer = new EnumSerializer();
    private final static ISerializerBase floatSerializer = new FloatSerializer();
    private final static ISerializerBase int16Serializer = new Int16Serializer();
    private final static ISerializerBase int32Serializer = new Int32Serializer();
    private final static ISerializerBase int64Serializer = new Int64Serializer();
    private final static ISerializerBase listSerializer = new ListSerializer();
    private final static ISerializerBase mapSerializer = new MapSerializer();
    private final static ISerializerBase nullSerializer = new NullSerializer();
    private final static ISerializerBase objectSerializer = new ObjectSerializer();
    private final static ISerializerBase stringSerializer = new StringSerializer();

    public static ISerializerBase getSerializer(Class<?> type) throws ClassNotFoundException, ForbidSerializeException ,RsProtocolException{
        if (type == null) {
            return nullSerializer;
        } else if (type.isEnum()) {
            return enumSerializer;
        }
        int typeId = CommonDataTypeMap.getTypeId(type);
        ISerializerBase serializer = null;
        switch (typeId) {
            case 0:
            case 1:
                serializer = nullSerializer;
                break;
            case 2:
                serializer = objectSerializer;
                break;
            case 3:
                serializer = boolSerializer;
                break;
            case 4:
                serializer = charSerializer;
                break;
            case 5:
            case 6:
                serializer = byteSerializer;
                break;
            case 7:
            case 8:
                serializer = int16Serializer;
                break;
            case 9:
            case 10:
                serializer = int32Serializer;
                break;
            case 11:
            case 12:
                serializer = int64Serializer;
                break;
            case 13:
                serializer = floatSerializer;
                break;
            case 14:
                serializer = doubleSerializer;
                break;
            case 15:
                serializer = decimalSerializer;
                break;
            case 16:
                serializer = dateTimeSerializer;
                break;
            case 18:
                serializer = stringSerializer;
                break;
            case 19:
            case 20:
            case 21:
                serializer = listSerializer;
                break;
            case 23:
                serializer = arraySerializer;
                break;
            case 24:
            case 25:
                serializer = mapSerializer;
                break;
            default:
                serializer = objectSerializer;
        }
        return serializer;
    }
}
